<?php
class PhpView extends ViewHelper 
{
	/**
	 * 布局模版
	 *
	 * @var string
	 */
	protected $layout;
	
	
	/**
	 * 获取模版完整路径
	 *
	 * @return String
	 */
	public function getTemplatePath()
	{
		if ($this->getTemplate()) return YucyConfig::get('template_path') . str_replace('.', DS, $this->getTemplate()) . "." . YucyConfig::get('view_template_suffix');
		
		return YucyConfig::get('template_path') . strtolower(Yucy::getApp()->getDispatcher()->getModuleName()) . DS . strtolower(Yucy::getApp()->getDispatcher()->getControllerName()) . DS . strtolower(Yucy::getApp()->getDispatcher()->getActionName()) . "." . YucyConfig::get('view_template_suffix');
	}
	
	
	/**
	 * 设置布局的路径
	 *
	 * @param string $layout 格式：index.index
	 */
	public function setLayout($layout)
	{
		$this->layout = $layout;
	}
	
	
	public function getLayout()
	{
		return $this->layout;
	}
	
	
	public function getLayoutPath()
	{
		if (!$this->layout) return NULL;
		
		return YucyConfig::get('template_path') . str_replace('.', DS, $this->layout) . YucyConfig::get('view_template_suffix');
	}
    
    
    /**
     * 获取内容
     *
     */
    private function _getBody()
    {
    	ob_start();
    	extract($this->value, EXTR_OVERWRITE);
    	if ($this->layout)
    	{
    		include($this->getLayoutPath());
    	}
    	else 
    	{
    		include($this->getTemplatePath());
    	}
    	return ob_get_clean();
    }
    
    
    /**
     * 输出内容
     */
    public function output()
    {
    	Yucy::getApp()->getResponse()->setHeader('Content-type', $this->getContentType(). '; charset=' . $this->getCharset());
    	Yucy::getApp()->getResponse()->setContent($this->_getBody())->send();
    }
}
?>